package com.wanfu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanfu.dao.ArticleMapper;
import com.wanfu.entity.Article;
import com.wanfu.service.ArticleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public int add (Article article) {
        return articleMapper.insertSelective(article);
    }


    @Override
    public int select(int id){
        return  articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update (Article aritcle) {
        return articleMapper.updateByPrimaryKeySelective(aritcle);
    }

    @Override
    public int delete (int id) {
        return articleMapper.deleteByPrimaryKey(id);
            }

    @Override
    public PageInfo<Article> listByTime(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articleList = articleMapper.selectBySqlKey(" and stuats=0 order by create_time desc");
        PageInfo<Article> articlePageInfo = new PageInfo<>(articleList);
        return articlePageInfo;
    }

    @Override
    public PageInfo<Article> listByCategory (int pageNum, int pageSize, int categoryId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articleList = articleMapper.selectBySqlKey(" and a.stuats=0 and atc.id=" + categoryId + " order by a.create_time desc");
        PageInfo<Article> articlePageInfo = new PageInfo<>(articleList);
        return articlePageInfo;
    }
}
